package com.choosefine.it.manager.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.choosefine.it.common.def.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.IdsUtil;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.manager.model.ManagerPublishFindTeamInfo;
import com.choosefine.it.manager.model.dto.SelectFindTeamInfoByConditionDTO;
import com.choosefine.it.manager.model.dto.UpdateManagerFindTeamVerifyStatusDTO;
import com.choosefine.it.manager.model.vo.ManagerPublishFindTeamInfoVO;
import com.choosefine.it.manager.service.ManagerPublishFindTeamInfoService;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 项目经理发布找班组相关
 * @author juchengyang 2017年3月4日
 *
 */
@RestController
@RequestMapping("/managerPublishFindTeamInfo")
@Api(value = "项目经理发布找班组相关", description = "鞠成阳")
public class ManagerPublishFindTeamInfoController extends BaseController {
	
	@Autowired
	private ManagerPublishFindTeamInfoService managerPublishFindTeamInfoService;

	
	/**
	 * 控制台查询项目经理找班组列表
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "控制台查询项目经理找班组列表")
	@RequestMapping(value = "/selectManagerPublishTeamByCondition", method = RequestMethod.POST)
	public String selectManagerPublishTeamByCondition(@RequestBody SelectFindTeamInfoByConditionDTO dto) {
		String result = null;
		if (null != dto.getPageNum() && null != dto.getPageSize() && null != dto.getVerifyStatus()) {
			Page<ManagerPublishFindTeamInfo> resultPage = null;
			Map<String, Object> params = new HashMap<>();
			params.put("verifyStatus", dto.getVerifyStatus());
			if (null != dto.getEndTime()){
				params.put("endTime", dto.getEndTime());
			}
			if (null != dto.getStartTime()){
				params.put("startTime", dto.getStartTime());
			}
			if (StringUtils.isNotBlank(dto.getProjectName())){
				params.put("projectName", dto.getProjectName());
			}
			if (StringUtils.isNotBlank(dto.getTeamTypeName())){
				params.put("teamTypeName", dto.getTeamTypeName());
			}
			if (StringUtils.isNotBlank(dto.getUserCode())){
				params.put("userCode", dto.getUserCode());
			}
			resultPage = managerPublishFindTeamInfoService.selectManagerPublishTeamByCondition(params, dto.getPageNum(), dto.getPageSize());
			result = responseArraySuccess(resultPage.toPageInfo());
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	/**
	 * 控制台 审核操作
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "审核发布信息")
	@RequestMapping(value = "/updateManagerPublishFindTeamInfoVerifyStatus", method = RequestMethod.PUT)
	public String updateManagerPublishFindTeamInfoVerifyStatus(@RequestBody UpdateManagerFindTeamVerifyStatusDTO updateManagerFindTeamVerifyStatusDTO) {
		String result = null;
		Map<String, Object> params = new HashMap<>();
		if(updateManagerFindTeamVerifyStatusDTO.getId() != null
				&& null != updateManagerFindTeamVerifyStatusDTO.getVerifyStatus()
				&& null != updateManagerFindTeamVerifyStatusDTO.getPublisher()){
			params.put("id", updateManagerFindTeamVerifyStatusDTO.getId());
			params.put("verifyStatus", updateManagerFindTeamVerifyStatusDTO.getVerifyStatus());
			params.put("publisher", updateManagerFindTeamVerifyStatusDTO.getPublisher());
			params.put("utime", DateUtils.getNowTime());
			if(!StringUtils.isEmpty(updateManagerFindTeamVerifyStatusDTO.getReason())){
				params.put("reason", updateManagerFindTeamVerifyStatusDTO.getReason());
			}
			int num = managerPublishFindTeamInfoService.updateManagerPublishFindTeamInfoVerifyStatus(params);
			if (num > 0){
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	/**
	 * 根据id查询详情
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "根据记录的id查看发布信息详情", notes = "")
	@ApiImplicitParam(name = "id", value = "发布信息Id", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/selectManagerPublishFindTeamById/{id}", method = RequestMethod.GET)
	public String selectManagerPublishFindTeamById(@PathVariable Integer id) {
		String result = null;
		if (null != id){
			ManagerPublishFindTeamInfo resultModel = managerPublishFindTeamInfoService.selectManagerPublishFindTeamById(id);
			if (null != resultModel) {
				result = responseSuccess(resultModel);
			} else {
				result = responseFail(ResultCode.MSG_DATA_EMPTY);
			}
		} else {
			result = responseSuccess(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	/**
	 * 根据id编辑发布信息
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "根据记录的id更新该条发布信息", notes = "")
	@RequestMapping(value = "/updateManagerPublishFindTeamInfo", method = RequestMethod.PUT)
	public String updateManagerPublishFindTeamInfo(@RequestBody ManagerPublishFindTeamInfo model) {
		String result = null;
		if (null != model.getId()){
			int num = managerPublishFindTeamInfoService.updateManagerPublishFindTeamInfo(model);
			if (num > 0){
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	/**
	 * 项目经理发布找班组信息（代发布）
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "项目经理发布找班组信息（代发布）")
	@RequestMapping(method = RequestMethod.POST, value = "/insertManagerPublishFindTeamInfo")
	public String insertManagerPublishFindTeamInfo(@RequestBody ManagerPublishFindTeamInfo model) {
		String result = null;
		if (StringUtils.isNotBlank(model.getUserCode())) {
			int num = managerPublishFindTeamInfoService.insertManagerPublishFindTeamInfo(model);
			if (num > 0){
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	/**
	 * 根据id删除发布信息（可批量）
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "根据id删除发布信息（可批量）")
	@ApiImplicitParam(name = "idss", value = "发布信息Id(多个用“，”隔开)", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/deleteManagerFindTeam/{idss}", method = RequestMethod.GET)
	public String deleteManagerFindTeam(@PathVariable String idss) {
		List<Integer> ids = new ArrayList<>();
		String result = null;
		Map<String, Object> params = new HashMap<>();
		ids=IdsUtil.getIds(idss);
		if (ids.size() > 0) {
			params.put("ids", ids);
			params.put("utime", DateUtils.getNowTime());
			int num = managerPublishFindTeamInfoService.deleteManagerPublishFindTeamInfo(params);
			if (num > 0){
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	/**
	 * juchengyang 2017/3/4
	 */
	@ApiOperation(value = "修改某条发布招班组信息的发布状态", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "publishStatus", value = "发布状态0开始找1停止找", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "id", value = "该条记录的id", required = true, dataType = "Integer", paramType = "path") })
	@RequestMapping(value = "/updateManagerPublishFindTeamPublishStatus/{id}/{publishStatus}", method = RequestMethod.GET)
	public String updateManagerPublishFindTeamPublishStatus(@PathVariable Integer id, @PathVariable Integer publishStatus) {
		String result = null;
		if (null != id && null != publishStatus){
			Map<String, Object> params = new HashMap<>();
			params.put("id", id);
			params.put("publishStatus", publishStatus);
			int num = managerPublishFindTeamInfoService.updateManagerPublishFindTeamPublishStatus(params);
			if (num > 0){
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	/**
	 * juchengyang 2017/3/4 
	 */
	@ApiOperation(value = "根据用户的usercode查看发布的找班组列表", notes = "", response = ManagerPublishFindTeamInfo.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "用户的唯一编号", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "verifyStatus", value = "审核状态", required = true, dataType = "Integer", paramType = "path")})
	@RequestMapping(value = "/selectManagerPublishFindTeamByUserCode/{userCode}/{verifyStatus}/{pageNum}/{pageSize}", method = RequestMethod.GET)
	public String selectManagerPublishFindTeamByUserCode(@PathVariable String userCode, @PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable Integer verifyStatus) {
		Page<ManagerPublishFindTeamInfo> resultPage = null;
		if (null != pageNum && null != pageSize && null != verifyStatus && StringUtils.isNotBlank(userCode)){
			Map<String, Object> params = new HashMap<>();
			params.put("userCode", userCode);
			params.put("verifyStatus", verifyStatus);
			resultPage = managerPublishFindTeamInfoService.selectManagerPublishFindTeamByUserCode(params, pageNum, pageSize);
		}
		return responseArraySuccess(resultPage);
	}
	@RequestMapping(value = "/ProjectBy",method = RequestMethod.GET)
	/**
	 * 班组首页-找项目-找项目列表3.6yuxin
	 */
	@ApiOperation(value = "班组首页-找项目-找项目列表", response = ManagerPublishFindTeamInfoVO.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "region", value = "地区省或市", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "teamTypeIds", value = "班组种类id,只允许填一个", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "engCategoryId", value = "清包1，重包2", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "起始页", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页行", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "random", value = "项目名称或公司名称", required = false, dataType = "String", paramType = "query")
	})
	public String getLimit(String region, // 地区id,11.15dzq
						   String teamTypeIds, // 班组种类id
			Integer engCategoryId, // 清包1，重包2
			Integer pageNum, // 分页对象
			Integer pageSize,
			String random
			) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();// 插入分页数据
		param.put("region", region);
		param.put("teamTypeIds", teamTypeIds);
		param.put("engCategoryId", engCategoryId);
		param.put("random",random);
		return responseSuccess(managerPublishFindTeamInfoService.selectByContractAndRegionAndTeamType(param, pageNum,pageSize));
	}
	/** 
 * @Description：查询项目经理发布的项目详情
 * @Author：yuxin
 * @Create Date：2017/3/1/0001
 * @param：
 * @return：
 */
	@ApiOperation(value = "查询项目经理发布的项目详情", response = ManagerPublishFindTeamInfo.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode",value = "班组的code",dataType = "String",required = true,paramType = "path"),
			@ApiImplicitParam(name = "uuid",value = "uuid",dataType = "String",required = true,paramType = "path"),
			@ApiImplicitParam(name = "publishProjectId",value = "发布的id",dataType = "Integer",required = true,paramType = "path")
	})
	@RequestMapping(value = "/selectTeamJobs/{userCode}/{uuid}/{publishProjectId}", method = RequestMethod.GET)
	public String selectTeamJobs(@PathVariable String userCode,
									@PathVariable String uuid,
									@PathVariable Integer publishProjectId) {
			String result = null;
			Map map=new HashMap();
			map.put("userCode",userCode);
			map.put("uuid",uuid);
			map.put("publishId",publishProjectId);
			map.put("type", CommonConstant.TEAM_COLLECT_MANAGER);
		ManagerPublishFindTeamInfo managerPublishFindTeamInfo = managerPublishFindTeamInfoService.selectTeamJobsDetailById(map);
			if (managerPublishFindTeamInfo != null) {
				result = responseSuccess(managerPublishFindTeamInfo, ResultCode.SUCCESS, ResultCode.MSG_QUERY_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
			}
		return result;
	}
	
}
